<?php

/**
 * alltosun.com  合伙人相关接口
 * ============================================================================
 * 版权所有 (C) 2009-2015 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 张元昊 (zhangyh@alltosun.com) $
 * $Date: 2015-12-12 13:15:44 $
 * $Id$
 */
class Action
{

    private $token = '';
    private $user_id = 0;
    private $per_page = 50;
    private $user_info = array();
    private $api_stat_id = 0;
    private $store_info = array();
    private $role_user_info = array();

    public function __construct() {
        $this->token = an_request('token');

        if ($this->token) {
            $user_info = user_helper::get_user_info_by_token($this->token);
            if ($user_info && $user_info['status'] == 1) {
                $this->user_id = $user_info['id'];
                $this->user_info = $user_info;
            }
        }

        // 判断是否有权限
        if (!$this->user_id) {
            api_helper::return_data(1, '对不起，请您先登录');
        }
        $role_user_info = _uri('role_user', array('user_id' => $this->user_id));

        if (!$role_user_info) {
            api_helper::return_data(1, '对不起，您没有权限');
        }
        $this->role_user_info = $role_user_info;
        if ($role_user_info['role_id'] != 5) {
            $user_marketing_info = _model('user_marketing')->read(array(
                'marketing_id' => $this->user_id,
                'status' => 1
            ));
            if (!$user_marketing_info) {
                api_helper::return_data(1, '对不起，您没有权限');
            }
            $store_user_id = $user_marketing_info['agency_id'];
        } else {
            $store_user_id = $this->user_id;
        }

        $store_info = _uri('store', array('user_id' => $store_user_id));
        if (!$store_info) {
            api_helper::return_data(1, '商铺信息有误');
        }

        $this->store_info = $store_info;

    }

    /**
     * 删除市场人员
     */
    public function delete_marketing() {
        $marketing_id = an_request('marketing_id');
        // 加密参数
        $sign_param = array(
            'marketing_id' => $marketing_id,
        );

        // 验证密钥
        api_helper::check_sign($sign_param, 1);

        $res = _widget('dealers')->delete_marketing($this->user_info, $marketing_id);
        if ($res['flag']) {
            api_helper::return_data(0, $res['info']);
        } else {
            api_helper::return_data(1, $res['info']);
        }
    }

    /**
     * 新增和更新合伙人多仓库地址
     */
    public function delete_dealer_address() {
        $address_id = an_request('address_id', '', 'post');

        // 加密参数
        $sign_param = array(
            'address_id' => $address_id
        );

        if (!$address_id) {
            api_helper::return_data(1, '删除失败，请联系客服!');
        }

        // 验证密钥
        api_helper::check_sign($sign_param, 1);

        $flag = _widget('dealers')->delete_dealer_address($this->user_id, $address_id);
        if (!$flag) {
            api_helper::return_data(1, '删除失败!');
        }

        api_helper::return_data(0, '删除成功!');
    }

    /**
     * 新增和更新合伙人多仓库地址
     */
    public function update_dealer_address() {
        $province_code = an_request('province_code', '', 'post');
        $city_code = an_request('city_code', '', 'post');
        $county_code = an_request('county_code', '', 'post');
        $dealer_address = an_request('dealer_address', '', 'post');
        $dealer_name = an_request('dealer_name', '', 'post');
        $dealer_mobile = an_request('dealer_mobile', '', 'post');
        $address_id = an_request('address_id', '', 'post');

        // 加密参数
        $sign_param = array(
            'province_code' => $province_code,
            'city_code' => $city_code,
            'county_code' => $county_code,
            'dealer_address' => $dealer_address,
            'dealer_name' => $dealer_name,
            'dealer_mobile' => $dealer_mobile,
            'address_id' => $address_id
        );

        if (!$dealer_mobile) {
            api_helper::return_data(1, '仓库收货电话不能为空!');
        }

        if (!$dealer_name) {
            api_helper::return_data(1, '仓库收货人姓名不能为空');
        }

        if (!$dealer_address) {
            api_helper::return_data(1, '仓库收货详细地址不能为空');
        }

        // 验证密钥
        api_helper::check_sign($sign_param, 1);

        $flag = _widget('dealers')->update_address($province_code, $city_code, $county_code, $dealer_address, $dealer_name, $dealer_mobile, $this->user_id, $address_id, 0);
        if (!$flag) {
            api_helper::return_data(1, '更新仓库地址失败');
        }

        api_helper::return_data(0, array('info' => '更新仓库地址成功!'));
    }

    /**
     * 编辑合伙人个人信息
     */
    public function update_dealer_detail() {
        $realname = an_request('realname');
        $weixin = an_request('weixin');
        $bank_code = an_request('bank_code');
        $bank_name = an_request('bank_name');

        // 加密参数
        $sign_param = array(
            'realname' => $realname,
            'weixin' => $weixin,
            'bank_code' => $bank_code,
            'bank_name' => $bank_name
        );
        //验证密钥
        api_helper::check_sign($sign_param, 1);

        $dealer_id = $this->user_info['id'];

        $result = _widget('dealers')->update_dealer_detail($this->user_info, $dealer_id, $realname, $weixin, $bank_code, $bank_name);

        if ($result['flag']) {
            api_helper::return_data(0, $result['info']);
        } else {
            api_helper::return_data(1, $result['info']);
        }
    }

    /**
     * 编辑市场人员信息
     */
    public function update_marketing_detail() {
        // 必填
        $marketing_id = an_request('marketing_id');
        $realname = an_request('realname');

        $bank_name = an_request('bank_name');
        $bank_cardno = an_request('bank_cardno');
        $idcard = an_request('idcard');
        $gender = an_request('gender');
        $email = an_request('email');
        $qq = an_request('qq');
        $weixin = an_request('weixin');
        $department = an_request('department');
        $job = an_request('job');
        //加密参数
        $sign_param = array(
            'marketing_id' => $marketing_id,
            'realname' => $realname,
            'bank_name' => $bank_name,
            'bank_cardno' => $bank_cardno,
            'idcard' => $idcard,
            'gender' => $gender,
            'email' => $email,
            'qq' => $qq,
            'weixin' => $weixin,
            'department' => $department,
            'job' => $job
        );
        //验证密钥
        api_helper::check_sign($sign_param, 1);

        $result = _widget('dealers')->update_marketing_detail($this->user_info, $marketing_id, $realname, $bank_name, $bank_cardno, $idcard, $gender, $email, $qq, $weixin, $department, $job);

        if ($result['flag']) {
            api_helper::return_data(0, $result['info']);
        } else {
            api_helper::return_data(1, $result['info']);
        }

    }

    /**
     * 市场人员的门店迁移
     */
    public function store_to_market() {
        // 店主用户ID,多选 主店
        $owner_ids = an_request('owner_ids', '', 'request', 0);

        // 市场人员ID
        $market_id = an_request('market_id', 0);
        $to_agency = an_request('to_agency', 0);

        $sign_param = array(
            'owner_ids' => $owner_ids,
            'market_id' => $market_id,
            'to_agency' => $to_agency
        );

        // 验签
        api_helper::check_sign($sign_param, 1);

        // 迁移到其他市场人员旗下
        if ($to_agency != -1) {

            $user_info = user_helper::get_user_info($market_id);
            if (!$user_info) {
                api_helper::return_data(1, '市场人员不存在!');
            }

            if (!$user_info['status']) {
                api_helper::return_data(1, '市场人员已经解约!');
            }

            if ($user_info['role_type'] != user_config::MARKETING) {
                api_helper::return_data(1, '指派的市场人员身份不正确!');
            }

            $market_info = dealers_helper::get_dealers_staff_info($market_id);
            if (!$market_info || $market_info['agency_id'] != $this->user_id) {
                api_helper::return_data(1, '指派市场人员失败!');
            }

        } else {
            // 迁移到合伙人旗下
            if ($this->role_user_info['role_id'] != user_config::AGENCY) {
                api_helper::return_data(1, '合伙人用户身份有误!');
            }
            $market_id = $this->user_id;
        }

        $owner_ids = json_decode($owner_ids, true);
        if (!$owner_ids) {
            api_helper::return_data(1, '迁移的门店数据有误!');
        }

        $result = _widget('dealers')->market_store_to_other($owner_ids, $market_id);
        if ($result[0]) {
            $msg = '成功迁移' . $result[0] . '家门店';
            api_helper::return_data(0, 'success', array('info' => $msg));
        }

        api_helper::return_data(1, $result[1]);
    }


    // 创建市场人员
    public function create_people() {
        // 必填
        $realname = an_request('realname');
        $mobile = an_request('mobile');

        // 非必填
        $bank_name = an_request('bank_name');
        $bank_cardno = an_request('bank_cardno');
        $idcard = an_request('idcard');
        $gender = an_request('gender');
        $email = an_request('email');
        $qq = an_request('qq');
        $weixin = an_request('weixin');
        $department = an_request('department');
        $job = an_request('job');
        //加密参数
        $sign_param = array(
            'realname' => $realname,
            'mobile' => $mobile,
            'bank_name' => $bank_name,
            'bank_cardno' => $bank_cardno,
            'idcard' => $idcard,
            'gender' => $gender,
            'email' => $email,
            'qq' => $qq,
            'weixin' => $weixin,
            'department' => $department,
            'job' => $job
        );
        //验证密钥
        api_helper::check_sign($sign_param, 1);
        if (!$realname) {
            api_helper::return_data(1, '请正确的填写真实姓名');
        }

        if (!$mobile) {
            api_helper::return_data(1, '请填写正确的手机号');
        }

        if (!user_helper::is_mobile($mobile)) {
            api_helper::return_data(1, '手机号格式不正确');
        }

        // if(!$idcard) {
        //     api_helper::return_data(1, '身份证号码无效');
        // }

        // if(!check_card_id($idcard)) {
        //     api_helper::return_data(1, '身份证格式不正确');
        // }

        $gender_list = user_config::$user_gender_type;
        if (!$gender_list[$gender]) {
            api_helper::return_data(1, '性别无效');
        }

        //提交数据
        $other_info = array(
            'realname' => $realname,
            'mobile' => $mobile,
            'gender' => $gender
        );

        if ($email) {
            if (!user_helper::is_email($email)) {
                api_helper::return_data(1, '邮箱格式不正确');
            }
            $other_info['email'] = $email;
        }
        if ($weixin) {
            $other_info['weixin'] = $weixin;
        }
        if ($qq) {
            $other_info['qq'] = $qq;
        }

        $user_info = _uri('user', array('user_name' => $mobile));
        if ($user_info) {
            $role_user_info = _uri('role_user', array('user_id' => $this->user_id));
            if (in_array($role_user_info['role_id'], array(5, 15, 20))) {
                api_helper::return_data(1, '此用户已经有其他身份了');
            }
            $marketing_info = _uri('user_marketing', array('marketing_id' => $user_info['id'], 'status' => 1));
            if ($marketing_info) {
                api_helper::return_data(1, '此用户已经是市场人员了');
            } else {
                $info = array(
                    'marketing_id' => $user_info['id'],
                    'agency_id' => $this->user_id,
                    'idcard' => $idcard,
                    'status' => 1
                );
                if ($department) {
                    $info['department'] = $department;
                }
                if ($job) {
                    $info['job'] = $job;
                }

                if ($bank_name) {
                    // $bank_type_list = user_config::$bank_type;
                    // if(!$bank_type_list[$bank_name]) {
                    //     api_helper::return_data(1, '银行名称无效');
                    // }
                    $info['bank_name'] = $bank_name;
                }

                if ($bank_cardno) {
                    $info['bank_cardno'] = $bank_cardno;
                }

                if ($idcard) {
                    $info['idcard'] = $idcard;
                }

                $marketing_id = _model('user_marketing')->create($info);
                if ($marketing_id) {
                    $return_info = user_helper::reset_password($user_info['id'], $other_info, 10);
                } else {
                    api_helper::return_data(1, '网络问题，请重新提交');
                }
            }
        } else {
            $other_info['user_name'] = $mobile;
            $other_info['store_parent_id'] = $this->user_id;
            $return_info = user_helper::create_user($other_info, 10);

            if ($return_info) {
                $marketing_info = array(
                    'marketing_id' => $return_info['user_id'],
                    'agency_id' => $this->user_id,
                    'idcard' => $idcard,
                    'status' => 1
                );
                if ($department) {
                    $marketing_info['department'] = $department;
                }
                if ($job) {
                    $marketing_info['job'] = $job;
                }
                if ($bank_name) {
                    // $bank_type_list = user_config::$bank_type;
                    // if(!$bank_type_list[$bank_name]) {
                    //     api_helper::return_data(1, '银行名称无效');
                    // }
                    $marketing_info['bank_name'] = $bank_name;
                }

                if ($bank_cardno) {
                    $marketing_info['bank_cardno'] = $bank_cardno;
                }

                if ($idcard) {
                    $marketing_info['idcard'] = $idcard;
                }

                $marketing_id = _model('user_marketing')->create($marketing_info);
                if (!$marketing_id) {
                    api_helper::return_data(1, '网络问题，请重新提交');
                }

            } else {
                api_helper::return_data(1, '网络问题，请重新提交');
            }
        }

        // 短信发送市场人员通知
        $replace_arr = array('name' => $realname, 'user_name' => $mobile, 'code' => $return_info['password']);
        aliysms_helper::sms_send('抱抱堂', $replace_arr, $mobile, 'add_marketing');
        api_helper::return_data(0, 'success', $return_info);
    }

    // 获取市场人员列表
    public function get_people_list() {
        // marketing_id 要过滤掉的市场人员
        $marketing_id = an_request('marketing_id', 0);

        // t 代表迁移数据的列表，要组装好合伙人的信息, 0,1,2
        // 0,不显示合伙人， 1 显示合伙人，2显示经销商，并且不限解约的人
        // 0、订单指派不显示当前合伙人
        // 1、迁移门店的时候，迁移市场人员下的门店触发，可以签给自己或着其他市场人员
        // 2、迁移门店的时候，不在指定市场人员列表下显示合伙人，迁移合伙人自己签约门店时，市场人员列表不显示合伙人自己

        $is_move_store_data = an_request('t', 0);

        // 验证密钥
        api_helper::check_sign(array(), 1);

        $filter = array(
            'agency_id' => $this->user_id,
        );

        // 移数据的列表和指派,不显示已解约的人员
        if ($is_move_store_data || $marketing_id) {
            $filter['status'] = 1;
        }

        $marketing_user_list = array();
        // 在迁移市场人员门店接口，选择市场人员的列表要加上合伙人
        if ($is_move_store_data == 1) {
            $marketing_user_list[] = dealers_helper::market_list_add_agency_info($this->user_info);
        }
        // 只显示未删除的
        $filter['is_delete'] = 1;
        $marketing_list = _model('user_marketing')->getList($filter, ' ORDER BY `status` DESC,`id` DESC');
        foreach ($marketing_list as $k => $v) {

            // 过滤掉签约的市场人员
            if ($v['marketing_id'] == $marketing_id) {
                continue;
            }

            $tmp_user_info = user_helper::format_user_info($v['marketing_id']);
            if (!$tmp_user_info) {
                continue;
            }

            if ($tmp_user_info['avatar']) {
                $tmp_user_info['avatar'] = user_helper::avatar($tmp_user_info['id']);
            }

            if (!$marketing_id) {
                $tmp_user_info = dealers_helper::market_stat_info($v['marketing_id'], $tmp_user_info);
            }

            if ($is_move_store_data) {
                $tmp_user_info['realname'] = dealers_helper::market_list_add_tag($tmp_user_info['realname']);
            }
            if (!$v['status']) {
                $tmp_user_info['realname'] .= "(已解约)";
            }

            // 市场人员解约状态
            $tmp_user_info['status'] = $v['status'];

            $marketing_user_list[] = $tmp_user_info;
        }

        api_helper::return_data(0, 'success', $marketing_user_list);
    }

    //获取市场人员详细信息
    public function get_people_detail() {
        $marketing_id = an_request('marketing_id');
        //加密参数
        $sign_param = array(
            'marketing_id' => $marketing_id
        );
        //验证密钥
        api_helper::check_sign($sign_param, 1);

        if (!$marketing_id) {
            api_helper::return_data(1, '参数无效');
        }

        $filter = array(
            'marketing_id' => $marketing_id,
            //                 'status'       => 1
        );

        $marketing_info = _model('user_marketing')->read($filter);

        if ($marketing_info) {
            if ($marketing_info['agency_id'] != $this->user_id) {
                api_helper::return_data(1, '这不是您的市场人员');
            }
            $user_info = _uri('user', $marketing_id);
        } else {
            api_helper::return_data(1, '没有此市场人员');
        }

        $return_info = array(
            'realname' => $user_info['realname'],
            'gender' => $user_info['gender'],
            'mobile' => $user_info['mobile'],
            'qq' => $user_info['qq'],
            'weixin' => $user_info['weixin'],
            'email' => $user_info['email'],
            'idcard' => $marketing_info['idcard'],
            'bank_name' => $marketing_info['bank_name'],
            'bank_cardno' => $marketing_info['bank_cardno'],
            'department' => $marketing_info['department'],
            'job' => $marketing_info['job'],
            'status' => $marketing_info['status'],
            'store_num' => store_helper::get_marketing_user_store_num($marketing_id)
        );
        if (!$user_info['status']) {
            $return_info['realname'] .= "(已解约)";
        }

        //本月业绩
        $return_info['month_sales_money'] = _widget('stat.api_stat')->get_marketing_sale_data($marketing_id, 'curr_month', 'price');
        //本月销量数目
        $return_info['month_sales_num'] = _widget('stat.api_stat')->get_marketing_sale_data($marketing_id, 'curr_month', 'box');

        api_helper::return_data(0, 'success', $return_info);
    }

    /**
     * 解约和恢复市场人员
     */
    public function change_people_status() {
        $status = Request::Post('marketing_status', 0);
        $marketing_id = Request::Post('marketing_id', 0);

        //加密参数
        $sign_param = array(
            'marketing_status' => $status,
            'marketing_id' => $marketing_id
        );
        //验证密钥
        api_helper::check_sign($sign_param, 1);

        $result = _widget('dealers')->change_people_status($this->user_id, $marketing_id, $status);
        if ($result['flag']) {
            api_helper::return_data(0, $result['info']);
        } else {
            api_helper::return_data(1, $result['info']);
        }
    }

    //修改市场人员详细信息
    public function save_people_info() {

        $marketing_id = an_request('marketing_id');
        $realname = an_request('realname');
        $mobile = an_request('mobile');

        //非必填
        $bank_name = an_request('bank_name');
        $bank_cardno = an_request('bank_cardno');
        $idcard = an_request('idcard');
        $gender = an_request('gender');
        $email = an_request('email');
        $qq = an_request('qq');
        $weixin = an_request('weixin');
        $department = an_request('department');
        $job = an_request('job');

        //加密参数
        $sign_param = array(
            'marketing_id' => $marketing_id,
            'realname' => $realname,
            'mobile' => $mobile,
            'bank_name' => $bank_name,
            'bank_cardno' => $bank_cardno,
            'idcard' => $idcard,
            'gender' => $gender,
            'email' => $email,
            'qq' => $qq,
            'weixin' => $weixin,
            'department' => $department,
            'job' => $job
        );
        //验证密钥
        api_helper::check_sign($sign_param, 1);

        if (!$marketing_id) {
            api_helper::return_data(1, '操作的市场人员无效[1]');
        }

        if (!$realname) {
            api_helper::return_data(1, '请正确的填写真实姓名');
        }

        if (!$mobile) {
            api_helper::return_data(1, '请填写正确的手机号');
        }

        if (!user_helper::is_mobile($mobile)) {
            api_helper::return_data(1, '手机号格式不正确');
        }

        // if(!$idcard) {
        //     api_helper::return_data(1, '身份证号码无效');
        // }

        // if(!check_card_id($idcard)) {
        //     api_helper::return_data(1, '身份证格式不正确');
        // }

        $gender_list = user_config::$user_gender_type;
        if (!$gender_list[$gender]) {
            api_helper::return_data(1, '性别无效');
        }

        $user_info = _uri('user', $marketing_id);

        if (!$user_info) {
            api_helper::return_data(1, '操作的市场人员无效[2]');
        }
        $marketing_info = _model('user_marketing')->read(array('marketing_id' => $marketing_id, 'status' => 1));
        if (!$marketing_info) {
            api_helper::return_data(1, '操作的市场人员无效[3]');
        }

        if ($marketing_info['agency_id'] != $this->user_id) {
            api_helper::return_data(1, '此用户不是您的市场人员');
        }
        //提交数据
        $other_info = array(
            'realname' => $realname,
            'mobile' => $mobile,
            'gender' => $gender
        );
        if ($email) {
            if (!user_helper::is_email($email)) {
                api_helper::return_data(1, '邮箱格式不正确');
            }
            $other_info['email'] = $email;
        }
        if ($weixin) {
            $other_info['weixin'] = $weixin;
        }
        if ($qq) {
            $other_info['qq'] = $qq;
        }

        _model('user')->update($marketing_id, $other_info);

        $marketing_update_info = array(
            'status' => 1
        );
        if ($department) {
            $marketing_update_info['department'] = $department;
        }
        if ($job) {
            $marketing_update_info['job'] = $job;
        }
        if ($bank_name) {
            // $bank_type_list = user_config::$bank_type;
            // if(!$bank_type_list[$bank_name]) {
            //     api_helper::return_data(1, '银行名称无效');
            // }
            $marketing_update_info['bank_name'] = $bank_name;
        }

        if ($bank_cardno) {
            $marketing_update_info['bank_cardno'] = $bank_cardno;
        }
        if ($idcard) {
            $marketing_update_info['idcard'] = $idcard;
        }
        _model('user_marketing')->update($marketing_info['id'], $marketing_update_info);
        api_helper::return_data(0, 'success', array());
    }

    //解约市场人员
    public function remove_people() {

        $marketing_id = an_request('marketing_id');
        $other_marketing_id = an_request('other_marketing_id');

        //加密参数
        $sign_param = array(
            'marketing_id' => $marketing_id,
            'other_marketing_id' => $other_marketing_id
        );
        //验证密钥
        api_helper::check_sign($sign_param, 1);

        if (!$marketing_id) {
            api_helper::return_data(1, '操作的市场人员无效[1]');
        }

        $marketing_info = _model('user_marketing')->read(array('marketing_id' => $marketing_id, 'status' => 1));
        if (!$marketing_info) {
            api_helper::return_data(1, '操作的市场人员无效[3]');
        }

        if ($marketing_info['agency_id'] != $this->user_id) {
            api_helper::return_data(1, '此用户不是您的市场人员');
        }

        if (!$other_marketing_id) {
            api_helper::return_data(1, '操作的市场人员无效[2]');
        }

        $other_marketing_info = _model('user_marketing')->read(array(
            'marketing_id' => $other_marketing_id,
            'status' => 1
        ));
        if (!$other_marketing_info) {
            api_helper::return_data(1, '操作的市场人员无效[4]');
        }

        if ($other_marketing_info['agency_id'] != $this->user_id) {
            api_helper::return_data(1, '此用户不是您的市场人员');
        }

        $affected = _model('user_marketing')->update(array('marketing_id' => $marketing_id), array('status' => 0));
        if ($affected) {
            _model('store')->update(array('marketing_id' => $marketing_id), array('marketing_id' => $other_marketing_id));
            _model('user_store')->update(array('marketing_id' => $marketing_id), array('marketing_id' => $other_marketing_id));

            api_helper::return_data(0, 'success', array());
        } else {
            api_helper::return_data(1, '网络问题，请重新提交');
        }
    }

    // 获取总部商品列表
    public function get_goods_list() {
        $page = an_request('page_no', 1);
        // init_stock  盘点库存
        $type = an_request('type', '');

        $app_version = an_request('v', 0);
        if ($app_version > 1.6) {
            $this->per_page = 50;
        }

        $sign_param = array(
            'page' => $page
        );

        // 验证密钥
        api_helper::check_sign($sign_param, 1);


        $filter = array(
            'status' => 1,
            'is_sale' => 1
        );

        // 如果总部下架，合伙人还能不能盘点
        if ($type == 'init_stock') {
            unset($filter['is_sale']);
            $filter['is_init'] = 1;
        }

        $user_store_info = store_helper::get_user_store_info($this->user_id);
        if (!$user_store_info) {
            api_helper::return_data(1, '合伙人商铺有误');
        }

        // 获取所属合伙人商品
        $store_filter = array('store_id' => $user_store_info['id']);
        $store_goods_ids = _model('store_goods_sku')->getFields('sku_id', $store_filter);

        $goods_list = array();
        $data_list = get_app_data_list('goods', $filter, ' ORDER BY `view_order` DESC, `id` DESC ', $page, $this->per_page);
        if (!$data_list) {
            api_helper::return_data(1, '暂无商品数据!');
        }

        $page_arr = $data_list['page'];
        $goods_list = $data_list['data'];

        if ($goods_list) {
            foreach ($goods_list as $k => $v) {

                if ($v['cover']) {
                    $cover_path = _uri('attachment', $v['cover'], 'path');
                    $goods_list[$k]['cover'] = _image($cover_path, 'list');
                }

                $goods_list[$k]['category_name'] = goods_helper::get_goods_category_by_goods_id($v['id'])['title'];

                // 已包含浮动加价
                $goods_list[$k]['sku_list'] = goods_helper::get_sku_list($v['id'], $user_store_info['addon_price']);

                foreach ($goods_list[$k]['sku_list'] as $key => $value) {
                    $stock = 0;
                    // 是否进货过商品
                    if (in_array($value['sku_id'], $store_goods_ids)) {
                        $filter = array(
                            'sku_id' => $value['sku_id'],
                            'store_id' => $user_store_info['id'],
                            'goods_id' => $value['goods_id']
                        );

                        // 查询这个商品的库存
                        $goods_sku_info = _uri('store_goods_sku', $filter);
                        if ($goods_sku_info) {
                            $stock = $goods_sku_info['stock'];
                        } else {
                            $stock = 0;
                        }
                    }

                    $goods_list[$k]['sku_list'][$key]['stock'] = $stock;

                    // 二级合伙人加价，允许的情况下
                    if ($this->user_info['is_diff_price'] && dealers_helper::get_dealers_level($this->user_id) == 2) {
                        $dealers_diff_price = $value['diff_price'];
                        // TODO bbcoin 向上取整
                        $dealers_diff_bbcoin = ceil($value['diff_bbcoin']);
                    } else {
                        $dealers_diff_price = 0;
                        $dealers_diff_bbcoin = 0;
                    }

                    // $goods_list[$k]['sku_list'][$key]['diff2_price'] = $dealers_diff_price;
                    $goods_list[$k]['sku_list'][$key]['price'] = num_keep_two_round($v['price'] + $dealers_diff_price);
                    // $goods_list[$k]['sku_list'][$key]['diff2_bbcoin'] = $dealers_diff_bbcoin;
                    $goods_list[$k]['sku_list'][$key]['bbcoin'] = (string)(ceil($v['bbcoin']) + $dealers_diff_bbcoin);
                }
            }
        }

        $result_data = array();
        if ($app_version > 1.6) {
            $result_data['data_list'] = $goods_list;
            $result_data['pages'] = $page_arr;

        } else {
            $result_data = $goods_list;
        }
        api_helper::return_data(0, 'success', $result_data);
    }

    // 获取合伙人现有的库存列表
    public function get_goods_stock() {
        $page = an_request('page', 1);

        //加密参数
        $sign_param = array(
            'page' => $page
        );
        //验证密钥
        api_helper::check_sign($sign_param, 1);

        // $store_info = _uri('store', array('user_id' => $this->user_id));
        $store_info = $this->store_info;

        //获取所属合伙人商品
        $store_filter = array('store_id' => $store_info['id']);

        $store_goods_ids = _model('store_goods')->getFields('goods_id', $store_filter);

        //获取总店信息
        $user_store_info = _uri('user_store', array('user_id' => $store_info['owner_id']));

        // $filter = array(
        //         'store_id' => $store_info['id']
        //     );
        $goods_list = array();
        $store_goods_count = _model('store_goods')->getTotal($store_filter);
        $store_goods_list = array();
        if ($store_goods_count) {
            $pager = new Pager($this->per_page);
            //先把商户的商品排在前面
            $filter = array(
                'id' => $store_goods_ids,
                'is_sale' => 1,
                'status' => 1
            );
            $order_str = ' ORDER BY `id` IN(' . join(',', $store_goods_ids) . ') DESC , `view_order` DESC ';
            $store_goods_list = _model('goods')->getList($filter, $order_str . $pager->getLimit($page));
            // $store_goods_list = _model('store_goods')->getList($filter, ' ORDER BY `add_time` DESC '.$pager->getLimit($page));
            foreach ($store_goods_list as $key => $value) {
                $filter = array(
                    'store_id' => $user_store_info['id'],
                    'goods_id' => $value['id']
                );
                $sku_list = _model('store_goods_sku')->getList($filter);
                // $goods_list[$key] = _uri('goods', $value['goods_id']);
                if ($store_goods_list[$key]['cover']) {
                    $cover_path = _uri('attachment', $store_goods_list[$key]['cover'], 'path');
                    $store_goods_list[$key]['cover'] = _image($cover_path, 'list');
                }
                $store_goods_list[$key]['category_name'] = _uri('category', $store_goods_list[$key]['category_id'], 'title');
                $store_goods_list[$key]['sku_list'] = $sku_list;
            }
        }
        api_helper::return_data(0, 'success', $store_goods_list);
    }

    // 修改商品单价并上架
    public function edit_goods_price() {
        $goods_list = an_request('goods_list', '', 'request', 0);
        $store_id = an_request('store_id');

        // 加密参数
        $sign_param = array(
            'goods_list' => $goods_list,
            'store_id' => $store_id
        );
        // 验证密钥
        api_helper::check_sign($sign_param, 1);

        if (!$store_id) {
            api_helper::return_data(1, '商户ID无效');
        }

        $goods_list = json_decode($goods_list, true);
        if (!$goods_list) {
            api_helper::return_data(1, '请传递商品具体信息参数');
        }

        // 先做一遍验证
        foreach ($goods_list as $k => $v) {
            if (!$v['goods_id']) {
                api_helper::return_data(1, '商品ID无效');
            }

            if (!$v['sku_id']) {
                api_helper::return_data(1, '商品ID的SKU无效');
            }

            if (!$v['edit_price']) {
                api_helper::return_data(1, '商品修改的价格无效');
            }

            if (!is_numeric($v['edit_price'])) {
                api_helper::return_data(1, '商品修改的价格格式不正确');
            }
        }

        $dealers_store_info = $this->store_info;
        if (!$dealers_store_info) {
            api_helper::return_data(1, '合伙人信息错误');
        }

        // 店铺信息
        $store_info = store_helper::get_store($store_id);
        foreach ($goods_list as $k => $v) {
            $filter = array(
                'goods_id' => $v['goods_id'],
                'sku_id' => $v['sku_id']
            );

            //获取合伙人总店信息
            $agency_user_store_info = _uri('user_store', array('user_id' => $dealers_store_info['user_id']));
            $filter['store_id'] = $agency_user_store_info['id'];
            //print_r($filter);

            $store_goods_sku_info = _uri('store_goods_sku', $filter);
            if (!$store_goods_sku_info) {
                // 合伙人还没有进货此商品 跳过本次更新
                continue;
            }

            // 获取总店店铺信息
            $user_store_info = _uri('user_store', array('user_id' => $store_info['owner_id']));
            $filter['store_id'] = $user_store_info['id'];
            // print_r($filter);
            $store_goods_sku_info = _uri('store_goods_sku', $filter);

            $edit_price = num_keep_two_round($v['edit_price']);
            if (!$store_goods_sku_info) {
                $filter['user_id'] = $user_store_info['user_id'];
                $filter['price'] = $edit_price;
                $filter['status'] = 1;
                goods_helper::create_store_goods_sku($filter);
            } else {
                goods_helper::update_store_goods_sku($store_goods_sku_info['id'], array(
                    'price' => $edit_price,
                    'status' => 1
                ));
            }
        }
        if ($this->user_id != $store_info['agency_id']) {
            $marketing_user_info = user_helper::get_user_info($this->user_id);
            $agency_user_info = user_helper::get_user_info($store_info['agency_id']);

            // 改价5分钟之内发一次短信
            global $mc_wr;

            $agency_user_id = $agency_user_info['id'];
            $key = $agency_user_id . '_send_message_limit';

            if (!$mc_wr->get($key)) {
                $mc_wr->set($key, '1', 60 * 4);
                _widget('order.message')->edit_goods_price_message($this->user_info, $store_info, $marketing_user_info, $agency_user_info);
            }
        }
        api_helper::return_data(0, 'success', array());
    }

    //下架商品
    // 此接口暂停试用 zhangyh
    //    public function shelves_goods() {
    //        api_helper::return_data(1, '此接口暂停试用');
    //        $goods_id = an_request('goods_id');
    //        $sku_id = an_request('sku_id');
    //
    //        //加密参数
    //        $sign_param = array(
    //            'goods_list' => $goods_list,
    //            'sku_id' => $sku_id
    //        );
    //        //验证密钥
    //        api_helper::check_sign($sign_param, 1);
    //
    //        if (!$goods_id) {
    //            api_helper::return_data(1, '商品ID无效');
    //        }
    //
    //        if (!$sku_id) {
    //            api_helper::return_data(1, '商品ID的SKU无效');
    //        }
    //
    //        // $store_info = _uri('store', array('user_id' => $this->user_id));
    //        $store_info = $this->store_info;
    //        if (!$store_info) {
    //            api_helper::return_data(1, '合伙人信息错误');
    //        }
    //
    //        $filter = array(
    //            'goods_id' => $goods_id,
    //            'sku_id' => $sku_id,
    //            'store_id' => $store_info['id']
    //        );
    //        $store_goods_sku_info = _uri('store_goods_sku', $filter);
    //        if (!$store_goods_sku_info) {
    //            api_helper::return_data(1, '合伙人没有此商品信息');
    //        }
    //
    //        $affected = _model('store_goods_sku')->update($filter, array('status' => 0));
    //
    //        if ($affected) {
    //            api_helper::return_data(0, 'success', array());
    //        } else {
    //            api_helper::return_data(1, '网络问题，请重新提交');
    //        }
    //    }

    // 调整总店浮动价格接口
    // 此接口暂停试用 zhangyh
    //    public function edit_addon_price() {
    //        api_helper::return_data(1, '此接口暂停试用');
    //        $owner_id = an_request('owner_id');
    //        $edit_addon_price = an_request('edit_addon_price');
    //
    //        //加密参数
    //        $sign_param = array(
    //            'owner_id' => $owner_id,
    //            'edit_addon_price' => $edit_addon_price
    //        );
    //        //验证密钥
    //        api_helper::check_sign($sign_param, 1);
    //
    //        if (!$owner_id) {
    //            api_helper::return_data(1, '总店ID无效');
    //        }
    //
    //        if (!is_numeric($edit_addon_price)) {
    //            api_helper::return_data(1, '商品浮动的价格格式不正确');
    //        }
    //        $store_info = $this->store_info;
    //        // $store_info = _uri('store', array('user_id' => $this->user_id));
    //        if (!$store_info) {
    //            api_helper::return_data(1, '合伙人信息错误');
    //        }
    //
    //        if ($owner_id == $this->user_id) {
    //            api_helper::return_data(1, '合伙人不可以修改自己');
    //        }
    //
    //        $user_store_info = _uri('user_store', array('user_id' => $owner_id, 'agency_id' => $this->user_id));
    //        if (!$user_store_info) {
    //            api_helper::return_data(1, '总店信息错误');
    //        }
    //
    //        $affected = _model('user_store')->update($user_store_info['id'], array('addon_price' => $edit_addon_price));
    //        if ($affected) {
    //            api_helper::return_data(0, 'success', array());
    //        } else {
    //            api_helper::return_data(1, '网络问题，请重新提交');
    //        }
    //
    //    }

    /**
     * 待处理的订单列表  xdq add
     */
    public function get_delivery_list() {
        $page = an_request('page', 1);
        $progress = an_request('progress');

        // 暂时未用到
        $user_store_id = an_request('user_store_id');
        // $app_version = an_request('v', 0);

        //加密参数
        $sign_param = array(
            'page' => $page,
            'progress' => $progress
        );

        //验证密钥
        api_helper::check_sign($sign_param, 1);

        if ($this->user_info['role_type'] == user_config::AGENCY) {
            // 如果是合伙人
            $filter = array(
                'agency_id' => $this->user_id,
                'status' => 1
            );
        } else {
            $filter = array(
                '(`marketing_id`' => $this->user_id,
                'OR `assigned_id`' => $this->user_id,
                ') AND `status`' => 1,
                'is_assigned' => '1'
            );
        }

        if (isset($user_store_id) && $user_store_id) {
            $user_store_info = store_helper::get_user_store_by_user_store_id($user_store_id);
            if ($user_store_info) {
                $owner_id = $user_store_info['user_id'];
                $store_ids = store_helper::get_store_ids_by_owner_id($owner_id);
                $filter['`store_id`'] = $store_ids;
            } else {
                $filter['store_id'] = 0;
            }
        }

        $progress_list = order_config::$store_progress;
        if ($progress) {
            $progress_arr = explode(',', $progress);
            foreach ($progress_arr as $key => $value) {
                if (!$progress_list[$value]) {
                    api_helper::return_data(1, '没有此订单状态');
                }
                $progress_arr[$key] = (int)$value;
            }
            $filter['progress'] = $progress_arr;
            // $progress_keys = join(',', $progress_arr);
        }

        $tmp_order_list = array();

        $page_arr = array('page_no' => 1, 'pages' => 1);

        //order -> 订单数
        $order_count = _model('order')->getTotal($filter);

        if (empty($order_count)) {
            // 无订单
            api_helper::return_data(1, '暂无订单记录!');
        }

        $pager = new Pager($this->per_page);
        if ($pager->generate($order_count)) {
            $page_arr['page_no'] = $page;
            $page_arr['pages'] = $pager->getPages();
        }

        $order_list = _model('order')->getList($filter, ' ORDER BY `id` DESC ' . $pager->getLimit($page));

        foreach ($order_list as $k => $order_info) {
            $order_info['order_no'] = order_helper::order_id_encode($order_info['id']);//订单id转订单编码

            // 获取商户的详细信息，包括市场人员和经销商的详细信息
            $store_info = store_helper::get_store_sub_info($order_info['store_id']);
            if (!$store_info) {
                continue;
            }
            // 派单获取新的默认送货人
            if ($order_info['original_agency_id'] > 0) {
                $store_info['marketing_user_info'] = user_helper::format_user_info($order_info['agency_id']);
            }
            $order_info['store_info'] = $store_info;

            // 取消订单的用户类别
            $user_role_id = user_helper::get_user_role_id($order_info['cancel_order_user_id']);
            switch (intval($user_role_id)) {
                case user_config::AGENCY:
                    $cancel_order_type = '合伙人';
                    break;
                case user_config::MARKETING:
                    $cancel_order_type = '市场人员';
                    break;
                case user_config::STORE_OWNER:
                case user_config::STORE_MANAGER:
                    $cancel_order_type = '掌柜';
                    break;
                default:
                    $cancel_order_type = '其他人员';
                    break;
            }
            $order_info['cancel_order_type'] = $cancel_order_type;

            // 指派人的信息
            if ($order_info['assigned_id']) {
                $assigend_user_info = user_helper::get_user_info($order_info['assigned_id']);
                if ($assigend_user_info) {
                    $order_info['assign_user_info'] = array(
                        'realname' => $assigend_user_info['realname'],
                        'id' => $assigend_user_info['id'],
                        'mobile' => $assigend_user_info['mobile'],
                    );
                }
            }

            // 获取订单分支信息
            $order_branch_info = order_helper::get_order_branch_by_order_id($order_info['id']);

            if (!empty($order_branch_info)) {
                $order_info['bbcoin'] = $order_branch_info['bbcoin'];
                $order_info['is_bbcoin_enable'] = $order_branch_info['is_bbcoin_enable'];
            } else {
                $order_info['bbcoin'] = 0;
                $order_info['is_bbcoin_enable'] = 0;
            }

            //xdq -1 //if has not payed, show credit amount and days
            if ($order_info['is_pay'] != 1) {
                //credit money
                $order_info['credit_price'] = $order_info['price'];

                //days
                $order_info['credit_day'] = floor((strtotime($order_info['add_time']) - strtotime(time())) / 86400);
            } else {
                $order_info['credit_price'] = 0;
                $order_info['credit_day'] = 0;
            }

            /* xdq  2016-07-01  是否代理 */
            $order_info['order_by_agent'] = 1;//假的，暂时不需要 2016-07-07

            /* xdq  2016-07-01  商户类型:'100' /'105'=> '电影院', '120' => '商超' */
            if ($store_info['type'] == 100 || $store_info['type'] == 105) {
                $order_info['store_info']['store_owner_type'] = 1;
            } else {
                if ($store_info['type'] == 120) {
                    $order_info['store_info']['store_owner_type'] = 2;
                } else {
                    $order_info['store_info']['store_owner_type'] = 0;
                }
            }

            //is_ious 是否使用赊账  0-不使用  1-使用
            $order_info['store_info']['allow_credit'] = $order_info['is_ious'];

            //xdq  2016-07-01-07-04  , 撤销缘由，is in database
            $order_info['cancel_order_reason'] = $order_info['reason'];

            //0-未上传  1-已上传未审核，2-审核通过 3-驳回
            //影院  开通赊账的  未上传送货单 - 1  其他都是0
            //$order_tmp_info['delivery_note_uploaded'] = 0;

            //是否已上传送货单
            $delivery_info = order_helper::get_order_delivery_note_info_by_order_id($order_info['id']);
            if (isset($delivery_info['status'])) {
                // 如果是预审核显示为待审
                if ($delivery_info['status'] == '5') {
                    $delivery_info['status'] = '1';
                }
                $order_info['delivery_note_status'] = $delivery_info['status'];
                $order_info['delivery_info'] = $delivery_info;
            } else {
                $order_info['delivery_note_status'] = null;
            }

            //            if ($order_info['store_info']['service_agency_id'] > 0) {
            //                api_helper::_p($order_info);
            //            }

            $tmp_order_list[] = $order_info;
        }

        $return_data = array(
            'data_list' => $tmp_order_list,
            'pages' => $page_arr,
        );

        api_helper::return_data(0, 'success', $return_data);
    }

    /**
     * 合伙人指派订单(新) @author gaoa@alltosun.com
     */
    public function dealer_assign_order() {
        $order_no = an_request('order_no');
        $assigned_id = an_request('assigned_id');
        $other_assigned_name = an_request('other_assigned_name');
        $other_assigned_mobile = an_request('other_assigned_mobile');

        // 加密参数
        $sign_param = array(
            'order_no' => $order_no,
            'assigned_id' => $assigned_id,
            'other_assigned_name' => $other_assigned_name,
            'other_assigned_mobile' => $other_assigned_mobile
        );

        // 验证密钥
        api_helper::check_sign($sign_param, 1);

        $res = _widget('dealers')->dealer_assign_order($this->user_info, $order_no, $assigned_id, $other_assigned_name, $other_assigned_mobile);
        if ($res['flag']) {
            api_helper::return_data(0, $res['info']);
        } else {
            api_helper::return_data(1, $res['info']);
        }
    }

    // 指派任务
    public function assigned_order() {
        $order_no = an_request('order_no');
        $assigned_id = an_request('assigned_id');
        $other_assigned_name = an_request('other_assigned_name');
        $other_assigned_mobile = an_request('other_assigned_mobile');

        // 加密参数
        $sign_param = array(
            'order_no' => $order_no,
            'assigned_id' => $assigned_id,
            'other_assigned_name' => $other_assigned_name,
            'other_assigned_mobile' => $other_assigned_mobile
        );

        // 验证密钥
        api_helper::check_sign($sign_param, 1);
        $assigned_id = (int)$assigned_id;

        if (!$order_no) {
            api_helper::return_data(1, '订单编号无效');
        }

        $order_id = order_helper::order_id_decode($order_no);

        if (!is_numeric($order_id)) {
            api_helper::return_data(1, $order_id);
        }

        $order_info = _uri('order', $order_id);

        if (!$order_info || !$order_info['status']) {
            api_helper::return_data(1, '订单不存在或者已经删除');
        }

        if ($order_info['agency_id'] != $this->user_id) {
            api_helper::return_data(1, '此订单您没有指派权限');
        }

        if ($order_info['is_assigned']) {
            api_helper::return_data(1, '此订单已经指派，请勿重复操作');
        }

        $store_info = _uri('store', $order_info['store_id']);
        if (!$store_info) {
            api_helper::return_data(1, '此订单下单商户信息有误');
        }

        // 原始订单所有者鉴别, 如果符合, 则当前所属处理人变更为指派的合伙人
        // 合伙人本人, 因为无法获取到市场人员, 首先匹配合伙人自己
        // $assigned_id 已经是指派的合伙人及其市场人员, 此项覆盖为处理当前操作
        if ($order_info['original_agency_id'] == $store_info['agency_id']) {
            $store_info['agency_id'] = $order_info['agency_id'];
            $store_info['marketing_id'] = $order_info['agency_id'];
        }

        $store_user_info = user_helper::get_user_info($store_info['user_id']);
        $owner_user_info = user_helper::get_user_info($store_info['owner_id']);
        $agency_user_info = user_helper::get_user_info($store_info['agency_id']);
        if (!$owner_user_info) {
            api_helper::return_data(1, '找不到总店信息!');
        }

        // 用户分派后的订单商品数量 + 当前订单商品的数量 < 当前合伙人的库存量，要提示出来
        $info = order_helper::is_order_enough_stock($order_id);
        if (isset($info['info']) && $info['info'] === false) {
            api_helper::return_data(1, $info['message']);
        }

        // 市场人员用户信息
        $marketing_user_info = user_helper::get_user_info($store_info['marketing_id']);
        if ($assigned_id) {
            // 获取指派人员的信息
            $assigned_market_info = user_helper::get_user_info($assigned_id);
            // 指派市场人员
            if ($assigned_id != $store_info['agency_id']) {

                $user_marketing_info = _model('user_marketing')->read(array(
                    'marketing_id' => $assigned_id,
                    'status' => 1
                ));

                if (!$user_marketing_info) {
                    api_helper::return_data(1, '没有此市场人员');
                }
                if ($user_marketing_info['agency_id'] != $this->user_id) {
                    api_helper::return_data(1, '指派人不是您的市场人员');
                }
            }

            $info = array(
                'is_assigned' => 1,
                'assigned_id' => $assigned_id
            );

            $affected = _model('order')->update($order_info['id'], $info);
            if ($affected) {

                // 店铺信息
                $sms_store_info['store_title'] = $store_info['title'] . $store_info['title_branch'];
                $sms_store_info['user_name'] = $store_user_info['realname'];
                $sms_store_info['mobile'] = $store_user_info['mobile'];
                $sms_store_info['order_no'] = order_helper::order_id_encode($order_id);
                $sms_store_info['user_id'] = $store_user_info['id'];

                // 默认签约的市场人员
                $sms_store_info['market_user_id'] = $marketing_user_info['id'];
                $sms_store_info['market_user_name'] = $marketing_user_info['realname'];
                $sms_store_info['market_mobile'] = $marketing_user_info['mobile'];

                // 指派的市场人员
                if ($assigned_market_info) {
                    $sms_market_info['market_user_name'] = $assigned_market_info['realname'];
                    $sms_market_info['market_user_id'] = $assigned_market_info['id'];
                    $sms_market_info['market_mobile'] = $assigned_market_info['mobile'];
                } else {
                    $sms_market_info['market_user_name'] = '';
                    $sms_market_info['market_user_id'] = '';
                    $sms_market_info['market_mobile'] = '';
                }

                // 合伙人
                $sms_agency_info['agency_mobile'] = $agency_user_info['mobile'];
                $sms_agency_info['agency_id'] = $agency_user_info['id'];
                $sms_agency_info['user_name'] = $agency_user_info['realname'];

                _widget('dealers.message')->assigned_order_message($sms_store_info, $sms_market_info, $sms_agency_info, $assigned_id);

                // 指派人员是合伙人自己，默认自动发货
                if ($assigned_id == $store_info['agency_id']) {
                    _widget('order')->update_order_progress($order_no, 5, '', $this->user_info);
                }

                api_helper::return_data(0, 'success', array());
            } else {
                api_helper::return_data(1, '网络错误，请重试');
            }
        } else {

            if (!$other_assigned_name) {
                api_helper::return_data(1, '指派人姓名必须填写');
            }

            if (!$other_assigned_mobile) {
                api_helper::return_data(1, '指派人手机号必须填写');
            }

            if (!user_helper::is_mobile($other_assigned_mobile)) {
                api_helper::return_data(1, '指派人手机号格式不正确');
            }

            $info = array(
                'is_assigned' => 1,
                'other_assigned_name' => $other_assigned_name,
                'other_assigned_mobile' => $other_assigned_mobile
            );

            $affected = _model('order')->update($order_info['id'], $info);
            if ($affected) {

                // 店铺信息
                $sms_store_info['store_title'] = $store_info['title'] . $store_info['title_branch'];
                $sms_store_info['user_name'] = $store_user_info['realname'];
                $sms_store_info['mobile'] = $owner_user_info['mobile'];
                $sms_store_info['order_no'] = order_helper::order_id_encode($order_id);
                $sms_store_info['user_id'] = $store_user_info['id'];

                // 默认签约的市场人员
                $sms_store_info['market_user_id'] = $marketing_user_info['id'];
                $sms_store_info['market_user_name'] = $marketing_user_info['realname'];
                $sms_store_info['market_mobile'] = $marketing_user_info['mobile'];

                // 指派的市场人员
                $sms_market_info['market_user_name'] = $other_assigned_name;
                $sms_market_info['market_user_id'] = 0;
                $sms_market_info['market_mobile'] = $other_assigned_mobile;

                // 合伙人
                $sms_agency_info['agency_mobile'] = $agency_user_info['mobile'];
                $sms_agency_info['agency_id'] = $agency_user_info['id'];
                $sms_agency_info['user_name'] = $agency_user_info['realname'];

                _widget('dealers.message')->assigned_order_message($sms_store_info, $sms_market_info, $sms_agency_info, 0);
                api_helper::return_data(0, 'success', array());

            } else {
                api_helper::return_data(1, '网络错误，请重试');
            }
        }
    }

    // 合伙人订单提交接口
    public function trade_order() {
        // 进货商品ID
        $order_goods_list = an_request('order_goods_list', '', 'request', 0);

        $store_id = an_request('store_id');
        $price = an_request('price');
        $pay_type = an_request('pay_type');

        // 收货方式
        $deliver_type = an_request('deliver_type');
        $order_type = an_request('order_type');

        // 杯子比例
        $cup_list = an_request('cup_list', '', 'request', 0);

        // 订单备注
        $app_version = an_request('v', 0);

        // shinate 2016-07-22
        $user_money = an_request('user_money', '0.00');
        $is_user_money = an_request('is_user_money', 0);
        $bbcoin = an_request('bbcoin', '0');

        // shinate 2016-07-27 抱抱币兑换
        $bbcoin_price = an_request('bbcoin_price', '0');
        $order_bbcoin_exchange_list = an_request('order_bbcoin_exchange_list', '', 'request', 0);

        if ($app_version > 1.6) {
            $note = an_request('note');
        } else {
            $note = Request::Post('note');
        }

        // 合伙人仓库地址id(为兼容之前的版本所以不参与加密)
        $dealer_address_id = an_request('dealer_address_id');

        // 加密参数
        $sign_param = array(
            'order_goods_list' => $order_goods_list,
            'store_id' => $store_id,
            'price' => $price,
            'pay_type' => $pay_type,
            'deliver_type' => $deliver_type,
            'order_type' => $order_type,
            'cup_list' => $cup_list,
            'note' => $note,
            'is_user_money' => $is_user_money,
            'user_money' => $user_money,
            'bbcoin' => $bbcoin,
            'bbcoin_price' => $bbcoin_price,
            'order_bbcoin_exchange_list' => $order_bbcoin_exchange_list,
        );

        // 验证密钥
        api_helper::check_sign($sign_param, 1);

        if (!$order_goods_list) {
            api_helper::return_data(1, '请传递商品具体信息参数');
        }

        $order_goods_list = json_decode($order_goods_list, true);

        if (!$store_id) {
            api_helper::return_data(1, '合伙人商铺ID无效');
        }

        // $store_info = store_helper::get_store($store_id);
        $store_info = store_helper::get_store($store_id);
        if (!$store_info || $store_info['status'] == 0) {
            api_helper::return_data(1, '合伙人不存在或者已删除');
        }

        if ($store_info['user_id'] != $this->user_id) {
            if ($store_info['owner_id'] != $this->user_id) {
                api_helper::return_data(1, '合伙人商户所属用户与登录用户不符');
            }
        }

        $user_info = user_helper::get_user_info($store_info['user_id']);

        // 余额校验
        if ($is_user_money == 1) {
            $user_money = floatval($user_money);

            if ($user_money !== floatval($user_info['user_money'])) {
                api_helper::return_data(1, '您的余额发生了变化，请重新下单', array(
                    'user_money' => $user_info['user_money'],
                    'bbcoin' => $user_info['bbcoin']
                ));
            }
        }

        if (!empty($order_bbcoin_exchange_list)) {
            $bbcoin = intval($bbcoin);
            if ($bbcoin !== intval($user_info['bbcoin'])) {
                api_helper::return_data(1, '您的抱抱币发生了变化，请重新下单', array(
                    'user_money' => $user_info['user_money'],
                    'bbcoin' => $user_info['bbcoin']
                ));
            }
        }

        // 获取总店信息
        // $user_store_info = _uri('user_store', array('user_id' => $store_info['owner_id']));
        $user_store_info = store_helper::get_user_store($store_info['owner_id']);

        //查询合伙人信息
        // $agency_store_info = _uri('store', array('user_id' => $store_info['agency_id']));

        $order_price_sum = 0.00;
        $order_goods_num = 0;

        // 散装商品数量
        $order_bulk_goods_num = 0;

        // TODO FUCK!! 二级合伙人差价，注意下面保存用到了这个变量
        $is_diff_price = 0;
        if (dealers_helper::get_dealers_level($this->user_id) == 2) {
            $is_diff_price = 1;
        }

        $diff_sum_price = 0;

        // 下单商品列表
        $order_data = array();

        foreach ($order_goods_list as $k => $v) {

            $goods_info = goods_helper::get_goods_info($v['goods_id']);

            if (!$goods_info || !$goods_info['status']) {
                api_helper::return_data(1, '包含商品不存在');
            }

            if (!$goods_info['is_sale']) {
                api_helper::return_data(1, '包含商品还未上架');
            }

            $diff_price = 0;
            $store_goods_sku_info = goods_helper::get_sku_info($v['sku_id'], $v['goods_id']);
            if ($store_goods_sku_info) {
                if (!$store_goods_sku_info['status']) {
                    api_helper::return_data(1, '包含商品不存在或者已经下架');
                }
                // 算出单件商品的总价格
                $sku_goods_price = $store_goods_sku_info['price'];

                // 二级合伙人的价格
                $diff_price = dealers_helper::get_dealers_goods_diff_price($this->user_id, $v['sku_id']);
                $sku_goods_price += $diff_price;

                $diff_sum_price += $diff_price * $v['goods_num'];
                $tmp_price = ($sku_goods_price + $user_store_info['addon_price']) * $v['goods_num'];
            } else {
                api_helper::return_data(1, '包含商品信息有误');
            }

            $order_price_sum = $order_price_sum + $tmp_price;
            // 构建order_relation_info 数据结构
            $order_data[] = array(
                'user_id' => $this->user_id,
                'goods_id' => $v['goods_id'],
                'sku_id' => $v['sku_id'],
                'goods_num' => $v['goods_num'],
                'addon_price' => $user_store_info['addon_price'],
                'goods_price' => $sku_goods_price,
                // 单品的价格
                'real_price' => ($sku_goods_price + $user_store_info['addon_price']) * $v['goods_num'],
                // 二级合伙人差价
                'diff_sum_price' => ($diff_price + $user_store_info['addon_price']) * $v['goods_num']
            );

            // 判断是否有散装商品，计算杯的数量
            if ($goods_info['category_id'] == goods_config::CATEGORY_BULK) {
                $order_bulk_goods_num = $order_bulk_goods_num + $v['goods_num'];
            }
            // 计算商品总数
            $order_goods_num = $order_goods_num + $v['goods_num'];
        }

        // 判断合伙人进货是否达标
        $goods_num_limit = order_config::$dealers_trade_order_limit;
        if ($order_goods_num < $goods_num_limit['min']) {
            api_helper::return_data(1, '起订量不能低于' . $goods_num_limit['min'] . '箱，特殊需求请联系总部');
        }

        if ($order_goods_num > $goods_num_limit['max']) {
            api_helper::return_data(1, '起订量不能超于' . $goods_num_limit['max'] . '箱，特殊需求请联系总部');
        }

        // 格式化价钱 保留两位小数
        $order_price_sum = num_keep_two_round($order_price_sum);
        if (!$price) {
            api_helper::return_data(1, '订单总价格无效');
        }

        if ($order_price_sum != num_keep_two_round($price)) {
            api_helper::return_data(1, '订单总价格不正确');
        }

        $pay_type_list = order_config::$pay_type;
        if (!$pay_type || !$pay_type_list[$pay_type]) {
            api_helper::return_data(1, '支付方式无效');
        }

        $deliver_type_list = order_config::$deliver_type;
        if (!$deliver_type || !$deliver_type_list[$deliver_type]) {
            api_helper::return_data(1, '配送方式无效');
        }

        if ($order_type != 1) {
            api_helper::return_data(1, '订单类型无效');
        }

        // 处理兑换
        $order_exchange_data = array();
        $order_exchange_num = 0;
        $order_exchange_bbcoin_sum = 0;
        $order_exchange_diff_bbcoin_sum = 0;
        if (!empty($order_bbcoin_exchange_list)) {
            $exange_list = json_decode($order_bbcoin_exchange_list, 1);

            foreach ($exange_list as $item) {
                $goods_info = goods_helper::get_goods_info($item['goods_id']);
                if (!$goods_info || !$goods_info['status']) {
                    api_helper::return_data(1, '兑换的商品不存在');
                }
                if (!$goods_info['is_sale']) {
                    api_helper::return_data(1, '兑换的商品中存在还未上架商品');
                }

                $diff_bbcoin = 0;
                $sku_info = goods_helper::get_sku_info($item['sku_id'], $item['goods_id']);
                if ($sku_info) {
                    if (!$sku_info['status']) {
                        api_helper::return_data(1, '兑换的商品中包含不存的商品或者商品已经下架');
                    }
                    $sku_bbcoin = ceil($sku_info['bbcoin']);

                    // 算出单件商品的总价抱抱币
                    // 二级合伙人兑换抱抱币的加值
                    $diff_bbcoin = dealers_helper::get_dealers_goods_diff_bbcoin($this->user_id, $item['sku_id']);
                    $sku_bbcoin += $diff_bbcoin;

                    $order_exchange_diff_bbcoin_sum += $diff_bbcoin * $item['goods_num'];
                    $tmp_bbcoin = ($sku_bbcoin + $user_store_info['addon_price']) * $item['goods_num'];
                } else {
                    api_helper::return_data(1, '兑换的商品中包含有误的商品信息');
                }

                // 计算兑换商品总数
                $order_exchange_num += $item['goods_num'];
                // 总抱抱币累计
                $order_exchange_bbcoin_sum += $tmp_bbcoin;

                // 构建order_exchange_info 数据结构
                $order_exchange_data[] = array(
                    'user_id' => $this->user_id,
                    'goods_id' => $item['goods_id'],
                    'sku_id' => $item['sku_id'],
                    'goods_num' => $item['goods_num'],
                    'addon_price' => $user_store_info['addon_price'],
                    'goods_price' => $sku_bbcoin,
                    'exchange_type' => isset($item['exchange_type']) ? $item['exchange_type'] : 'bbcoin',
                    // 单品总币和
                    'real_price' => ($sku_bbcoin + $user_store_info['addon_price']) * $item['goods_num'],
                    // 二级合伙人加币
                    'diff_sum_price' => ($diff_bbcoin + $user_store_info['addon_price']) * $item['goods_num'],
                );
            }
        }

        if (intval($order_exchange_bbcoin_sum) != intval($bbcoin_price)) {
            api_helper::return_data(1, '兑换总价格不正确');
        }

        // 处理散装商品杯型
        if ($order_bulk_goods_num) {
            if (!$cup_list) {
                api_helper::return_data(1, '请传递散装商品具体信息参数');
            }

            $tmp_box_sum = 0;
            $cup_list = json_decode($cup_list, true);
            $cup_config_list = cup_config::$cup_type;

            // 组装信息格式
            $box_type_box = array();
            foreach ($cup_list as $k => $v) {

                if (!$v['box_num']) {
                    unset($cup_list[$k]);
                    continue;
                }

                // 计算此杯型数目
                $tmp_box_sum = $tmp_box_sum + $v['box_num'];

                // 过滤掉不正确的杯型
                if (!$cup_config_list[$v['cup_type']]) {
                    api_helper::return_data(1, '散装商品杯型信息有误');
                }

                $box_type_box[$v['cup_type']] = $v['box_num'];
            }

            // 判断散装商品数量和总箱子数量是否吻合
            if ($tmp_box_sum != $order_bulk_goods_num) {
                api_helper::return_data(1, '散装商品数量信息错误');
            }

            // 查询订单数目
            $order_num = order_helper::get_store_order_num($store_id);
            if ($order_num > 3) {
                $is_double = 0;
            } else {
                $is_double = 1;
            }

            $cup_num_data = cup_helper::get_dealer_cup_num($store_id, $order_bulk_goods_num, $box_type_box, $is_double);
        }

        $info = array(
            'type' => $order_type,
            'price' => $order_price_sum,
            'user_id' => $this->user_id,
            'store_id' => $store_id,
            'deliver_type' => $deliver_type,
            'progress' => order_config::DEALER_WAIT_PAY,
            'pay_type' => $pay_type,
            'goods_num' => $order_goods_num,
            'exchange_num' => $order_exchange_num,
            'pay_bbcoin' => $order_exchange_bbcoin_sum,
        );

        if ($note) {
            $info['note'] = $note;
        }

        $return_score_data = _widget('score_record')->format_store_order_score($store_id, $order_goods_num, $order_price_sum);
        if ($return_score_data) {
            $info['score'] = $return_score_data['score'];
        }

        // 差价标记
        $info['is_diff_price'] = $is_diff_price;
        // 差价总额
        $info['diff_sum_price'] = $diff_sum_price;
        // 差币总额
        $info['diff_sum_bbcoin'] = $order_exchange_diff_bbcoin_sum;

        // 测试账号标记
        if (user_helper::is_test_dealers_user($this->user_id)) {
            $info['is_enable'] = 0;
        }

        // 使用余额支付
        if ($is_user_money == 1 && $user_info['user_money'] > 0) {
            $real_user_money = floatval($user_info['user_money']);
            $order_price_sum = floatval($order_price_sum);
            $pay_user_money = $order_price_sum >= $real_user_money ? $real_user_money : $order_price_sum;

            // 余额付不清: order_config::DEALER_WAIT_PAY
            // 余额足矣付清: order_config::DEALER_WAIT_CONFIRM
            if ($order_price_sum > $pay_user_money) {
                $info['progress'] = order_config::DEALER_WAIT_PAY;
            } else {
                $info['progress'] = order_config::DEALER_WAIT_CONFIRM;
                $info['is_pay'] = 1;
                $info['pay_time'] = datetime();
            }
            $info['pay_user_money'] = $pay_user_money;
        }

        $order_id = _model('order')->create($info);
        if (!$order_id) {
            api_helper::return_data(1, '网络原因，提交订单失败');
        }

        if ($order_id) {

            // 成功再扣余额
            if (isset($info['pay_user_money']) && $info['pay_user_money'] > 0) {
                user_money_helper::change_user_money(array(
                    'user_id' => $user_info['id'],
                    'user_money' => -1 * $info['pay_user_money'],
                    'order_id' => $order_id,
                ));
                $user_info['user_money'] = _uri('user', $store_info['user_id'], 'user_money');
            }

            // 成功再扣抱抱币
            if (isset($info['pay_bbcoin']) && $info['pay_bbcoin'] > 0) {
                bbcoin_helper::change_bbcoin(array(
                    'user_id' => $user_info['id'],
                    'res_id' => bbcoin_config::RES_TYPE_EXCHANGE,
                    'bbcoin' => -1 * $info['pay_bbcoin'],
                    'order_id' => $order_id,
                ));
                $user_info['bbcoin'] = _uri('user', $store_info['user_id'], 'bbcoin');
            }

            //构架order_relation 关联表
            if (!empty($order_data)) {
                foreach ($order_data as $item) {
                    $item['order_id'] = $order_id;
                    _model('order_relation')->create($item);
                }
            }

            // 兑换表关联
            if (!empty($order_exchange_data)) {
                foreach ($order_exchange_data as $item) {
                    $item['order_id'] = $order_id;
                    // $item['add_time'] = datetime();
                    _model('order_exchange_relation')->create($item);
                }
            }

            // 增加到订单的分支表中
            order_helper::create_order_branch($order_id, $this->user_info);

            // 保存订单杯型数据
            if ($order_bulk_goods_num) {
                cup_helper::save_cup_record($order_id, $store_id, $cup_num_data);
            }

            // 订单行为记录
            order_helper::record_dealer_order_operation($order_id, $info['progress'], order_config::DEALER_ACTION_CREATE);

            // 更新订单收获地址与合伙人默认仓库
            store_helper::update_dealer_default_address($dealer_address_id, $order_id);

            $order_id_encode = order_helper::order_id_encode($order_id);
            _widget('dealers.message')->dealer_trade_order_message($order_id_encode, $store_info);

            // 进货完成更新 is_init
            store_helper::update_init_store($store_id);

            api_helper::return_data(0, 'success', array(
                'order_id' => $order_id_encode,
                'user_money' => $user_info['user_money'],
                'bbcoin' => $user_info['bbcoin']
            ));
        } else {
            api_helper::return_data(1, '网络原因，提交订单失败');
        }
    }

    // 盘点库存
    public function init_stock() {
        $desc = an_request('desc', '');
        $init_goods_list = an_request('init_goods_list', '', 'request', 0);
        $init_cup_list = an_request('init_cup_list', '', 'request', 0);


        $store_id = an_request('store_id');

        //加密参数
        $sign_param = array(
            'init_goods_list' => $init_goods_list,
            'store_id' => $store_id,
            'init_cup_list' => $init_cup_list
        );
        //验证密钥
        api_helper::check_sign($sign_param, 1);

        if (!$init_goods_list) {
            api_helper::return_data(1, '请传递商品具体信息参数');
        }

        $init_goods_list = json_decode($init_goods_list, true);
        $init_cup_list = json_decode($init_cup_list, true);

        if (!$store_id) {
            api_helper::return_data(1, '合伙人商铺ID无效');
        }

        if ($this->role_user_info['role_id'] != 5) {
            api_helper::return_data(1, '访问错误');
        }

        $return_status = _widget('order')->init_stock($init_goods_list, $store_id, $init_cup_list, $desc);
        if ($return_status && $return_status['info'] == 'success') {
            // 更新最后一次盘点时间
            dealers_helper::save_last_change_record($this->user_id, $store_id, 'init_stock');
            api_helper::return_data(0, 'success', array());
        } else {
            api_helper::return_data(1, $return_status['message']);
        }

    }

    // 合伙人仓库地址列表
    public function get_dealer_address_list() {
        // 验证密钥
        $dealer_address_list = _widget('user')->get_dealer_address_list($this->user_id);

        if ($dealer_address_list) {
            api_helper::return_data(0, 'success', $dealer_address_list);
        } else {
            api_helper::return_data(1, '暂无仓库地址数据');
        }
    }

    /**
     * TODO 即将废弃
     * 合伙人提交支付凭证
     */
    public function trade_payoff_voucher() {
        $order_no = Request::Post('order_id');
        $payoff_person = Request::Post('payoff_person');
        $payoff_money = Request::Post('payoff_money');
        $payoff_time = Request::Post('payoff_time');
        $payoff_extra = Request::Post('payoff_extra');

        //加密参数
        $sign_param = array(
            'order_id' => $order_no,
            'payoff_person' => $payoff_person,
            'payoff_money' => $payoff_money,
            'payoff_time' => $payoff_time,
            'payoff_extra' => $payoff_extra
        );

        //验证密钥
        api_helper::check_sign($sign_param, 1);

        $order_id = order_helper::order_id_decode($order_no);

        $result = _widget('dealers')->create_payoff($this->user_id, $order_id, $payoff_person, $payoff_money, $payoff_time, $payoff_extra);

        if ($result['flag']) {
            api_helper::return_data(0, $result['info']);
        } else {
            api_helper::return_data(1, $result['info']);
        }

    }

    /**
     * 确认到货
     */
    public function confirm_order_receive() {

        $order_id = an_request('order_id');

        // 加密参数
        $sign_param = array(
            'order_id' => $order_id,
        );

        // 验证密钥
        api_helper::check_sign($sign_param, 1);

        $ret = order_helper::DEALER_ACTION_DONE(order_helper::order_id_decode($order_id));

        if ($ret[0] === 0) {
            api_helper::return_data(0, 'success');
        } else {
            api_helper::return_data(1, $ret[1]);
        }
    }


    /**
     * 取消订单
     */
    public function cancel_order() {

        $order_id = an_request('order_id');
        $reason = an_request('reason');

        // 加密参数
        $sign_param = array(
            'order_id' => $order_id,
            'reason' => $reason,
        );

        // 验证密钥
        api_helper::check_sign($sign_param, 1);

        $ret = order_helper::DEALER_ACTION_CANCEL(order_helper::order_id_decode($order_id), $reason);

        if ($ret[0] === 0) {
            api_helper::return_data(0, 'success');
        } else {
            api_helper::return_data(1, $ret[1]);
        }
    }

    /**
     * xdq 上传送货单并插入数据库 2016-07-02
     * 订单和送货单是1对多的,一个订单一条记录
     * click submit
     */
    public function upload_delivery_note() {
        $order_no = an_request('order_id', '', 'post');
        $delivery_note_path = an_request('path', '', 'post');


        //$order_id      = Request::Post('order_id');
        //$delivery_note_path = Request::Post('path');

        //加密参数
        $sign_param = array(
            'order_id' => $order_no,
            'path' => $delivery_note_path
        );

        //验证密钥
        api_helper::check_sign($sign_param, 1);

        //xdq upload_img 公用保存图片api, which default is believed being successful
        //订单编码转化成订单ID
        $order_id = order_helper::order_id_decode($order_no);

        //判断order_delivery_note表是否有此订单记录
        $delivery_status = order_helper::get_order_delivery_note_status_by_order_id($order_id);
        if (!isset($delivery_status)) {
            api_helper::return_data(1, '送货单表无此记录');
        }

        //设置需要更新的送货单表的信息
        $insert_arr_deli = array(
            //'id'自增，不需要添加
            'order_id' => $order_id,
            'status' => 1,
            'path' => $delivery_note_path,
            'submit_time' => datetime()
        );

        $iRet = _model('order_delivery_note')->update(array('order_id' => $order_id), $insert_arr_deli);
        // 更新order中的状态
        _model('order')->update(array('id' => $order_id), array('delivery_note_status' => 1));
        if (!$iRet) {
            api_helper::return_data(1, '修改送货单表状态为已上传出错了');
        }

        api_helper::return_data(0, 'success', $iRet);

    }

    /**
     * 查询合伙人从上月1号开始截止到现在所有能开发票的订单,两个维度:按订单,按门店
     * @author xiaolin
     * @date   2016-08-29
     * @return json
     */
    public function make_invoice_order_list() {
        $classification = an_request('classification', 1); //分类方式:1,按订单,2按门店
        //加密参数
        $sign_param = array(
            'classification' => $classification
        );

        //验证是否可开票
        $is_invoice = invoice_helper::is_enable_make_invoice($this->user_id);
        if (!$is_invoice) {
            api_helper::return_data(1, '您还没有开票权限,请联系客服申请开通');
        }

        //验证接口
        api_helper::check_sign($sign_param, 1);

        //查询条件:
        //1,掌柜向该合伙人下的单,type为5
        //2,可赊账,is_allow_credit为1
        //3,的影院(包括私人影院),type(门店类型)为100,105
        //4,上个月1号到现在的订单,按照下单时间(add_time as order_time)
        //5,订单状态为配送完成(确认到货)的
        //6,没开过开票的订单make_invoice_status为0
        $last_month_date = date('Y-m-01 00:00:00', strtotime('-1 month')); //获取上月1号日期
        $last_tow_month_date = date('Y-m-01 00:00:00', strtotime('-2 month')); //获取上上月1号日期

        //$filter_store['is_allow_credit'] = 1;
        //$filter_store['type'] = array(100, 105);
        $filter_store['status !='] = 0;
        $filter_store['is_delete'] = 1;
        //$filter_store['is_enable'] = 1;

        $store_ids = _model('store')->getFields('id', enable_filter($filter_store));

        $filter_order['store_id'] = $store_ids ? $store_ids : '-1';
        //$filter_order['is_enable'] = 1;
        $filter_order['status'] = 1;
        $filter_order['type'] = 5;
        $filter_order['agency_id'] = $this->user_id;
        $filter_order['progress'] = order_config::STORE_DONE;
        $filter_order['make_invoice_status'] = 0;
        $filter_order['add_time >='] = (date('d') <= 10) ? $last_tow_month_date : $last_month_date;
        $filter_order['add_time <='] = date('Y-m-d H:i:s');

        $order_list = _model('order')->field('id', 'add_time', 'price', 'store_id')->getList(enable_filter($filter_order), 'order by `add_time` asc');

        $data['list'] = array();
        $data['date'] = date('m', strtotime('-1 month')) . '月1日';
        if (count($order_list)) {
            //转化订单号，及其他字段格式处理
            foreach ($order_list as $key => $value) {
                $order_list[$key]['title'] = store_helper::get_store_title($value['store_id'], 1);
                $order_list[$key]['order_no'] = order_helper::order_id_encode($value['id']);
                $order_list[$key]['is_select'] = '0'; //应董书长(iOS)要求添加
                $order_list[$key]['price'] = format_money($value['price']); //格式化金钱
                $order_list[$key]['order_time'] = $value['add_time']; // add_time as order_time
                unset($order_list[$key]['add_time']);
            }

            //按门店分类
            if ($classification == 2) {
                $temp = array();
                foreach ($order_list as $key => $value) {
                    $temp[$value['store_id']]['order_list'][] = $value;
                    $temp[$value['store_id']]['title'] = $value['title'];
                }
                $temp = array_values($temp); //只取value值
                $data['list'] = $temp;
            } else {
                $data['list'] = $order_list;
            }
        }
        api_helper::return_data(0, 'success', array('data' => $data));
    }
}